package com.regentsoft.cloudback.enterpriseAdmin.controller;

import com.regentsoft.cloudback.base.ResponseCode;
import com.regentsoft.cloudback.enterpriseAdmin.entity.ChannelInfo;
import com.regentsoft.cloudback.enterpriseAdmin.entity.CompanyAdminModel;
import com.regentsoft.cloudback.enterpriseAdmin.entity.CompanyInfo;
import com.regentsoft.cloudback.enterpriseAdmin.service.CompanyInfoService;
import com.regentsoft.cloudback.enterpriseAdmin.util.AdminTokenManager;
import com.regentsoft.cloudback.enterpriseAdmin.vo.ResultResp;
import com.regentsoft.cloudback.enterpriseAdmin.vo.req.ChannelAddInfo;
import com.regentsoft.cloudback.enterpriseAdmin.vo.req.ChannelAddReq;
import com.regentsoft.cloudback.enterpriseAdmin.vo.req.ChannelDeleteReq;
import com.regentsoft.cloudback.enterpriseAdmin.vo.req.ChannelUpdateReq;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by work on 2017/11/27.
 *
 * @author lumr
 */
@RestController
@RequestMapping(value = "api/company/")
public class CompanyInfoController {
    @Autowired
    private AdminTokenManager tokenManager;
    @Autowired
    private CompanyInfoService companyInfoService;

    @RequestMapping(value = "info", method = RequestMethod.GET)
    @ApiOperation(value = "获取企业基本信息", httpMethod = "GET", response = CompanyInfo.class)
    public ResultResp<CompanyInfo> getCompanyInfo(@RequestParam("REGENTTOKEN") String token) {
        CompanyAdminModel adminModel = tokenManager.get(token);

        CompanyInfo info = companyInfoService.getCompanyInfo(adminModel.getCode());
        if (info != null)
            return new ResultResp<>(info);

        return new ResultResp<>("没有企业信息");
    }

    @RequestMapping(value = "channelInfo", method = RequestMethod.GET)
    @ApiOperation(value = "获取企业授权店铺信息", httpMethod = "GET", response = ChannelInfo.class)
    public ResultResp<List<ChannelInfo>> getChannel(@RequestParam("REGENTTOKEN") String token){
        CompanyAdminModel adminModel = tokenManager.get(token);
        return new ResultResp<>(companyInfoService.getChannelInfo(adminModel.getCode()));
    }

    @RequestMapping(value = "unUsechannelInfo", method = RequestMethod.GET)
    @ApiOperation(value = "获取可授权店铺信息", httpMethod = "GET", response = ChannelInfo.class)
    public ResultResp<List<ChannelInfo>> getUnUseChannel(@RequestParam("REGENTTOKEN") String token){
        CompanyAdminModel adminModel = tokenManager.get(token);
        return new ResultResp<>(companyInfoService.getUnUseChannelInfo(adminModel.getCode()));
    }

    @RequestMapping(value = "deleteChannel", method = RequestMethod.POST)
    @ApiOperation(value = "删除企业授权店铺", httpMethod = "POST")
    public ResultResp deleteChannel(@RequestParam("REGENTTOKEN") String token, @RequestBody ChannelDeleteReq req){
        CompanyAdminModel adminModel = tokenManager.get(token);
        req.setCode(adminModel.getCode());
        return companyInfoService.deleteChannel(req);
    }

    @RequestMapping(value = "addChannel", method = RequestMethod.POST)
    @ApiOperation(value = "增加授权店铺", httpMethod = "POST")
    public ResultResp addChannel(@RequestParam("REGENTTOKEN") String token, @RequestBody ChannelAddReq req){
        CompanyAdminModel adminModel = tokenManager.get(token);
        for (ChannelAddInfo channel:req.getReq()){
            channel.setCode(adminModel.getCode());
            ResultResp resp = companyInfoService.addChannel(channel);
            if (!ResponseCode.SUCCESS.getCode().equals(resp.getCode()))
                return resp;
        }

        return ResultResp.SUCCESS;
    }

    @RequestMapping(value = "updateChannel", method = RequestMethod.POST)
    @ApiOperation(value = "修改授权店铺", httpMethod = "POST")
    public ResultResp updateChannel(@RequestParam("REGENTTOKEN") String token, @RequestBody ChannelUpdateReq req){
        CompanyAdminModel adminModel = tokenManager.get(token);
        req.setCompanyCode(adminModel.getCode());
        return companyInfoService.updateChannel(req);
    }
}
